package com.example.demo.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.OrdersMapper;
import com.example.demo.entity.*;
import com.example.demo.enums.OrderStatusEnum;
import com.example.demo.exception.ServiceException;
import com.example.demo.service.MallgoodsService;
import com.example.demo.service.OrderItemService;
import com.example.demo.service.OrdersService;

import com.example.demo.service.ShoppingcartService;
import com.example.demo.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2023-02-17 11:03:59
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{
@Autowired
private OrderItemService orderItemService;
@Autowired
private ShoppingcartService shoppingcartService;
@Autowired
private MallgoodsService mallgoodsService;
@Transactional
    @Override
    public void addOrder(List<Shoppingcart> carts) {


        for (Shoppingcart cart : carts) {
            BigDecimal total = new BigDecimal(0);
            total = total.add(cart.getPrice().multiply(BigDecimal.valueOf(cart.getNum())));
            Orders orders = new Orders();
            orders.setOrderno(IdUtil.fastSimpleUUID());
   orders.setUserid(TokenUtils.getCurrentUser().getId());
            orders.setStatus(OrderStatusEnum.NEED_PAY.getCode());
            orders.setTotal(total);  // 计算总价格
            save(orders); // 保存订单 获取订单id



            Mallgoods goods = mallgoodsService.getById(cart.getGoodid());
            if (goods.getNums() < cart.getNum()) {
                throw new ServiceException("-1", "库存不足");
            }
            goods.setNums(goods.getNums() - cart.getNum());
            mallgoodsService.updateById(goods);



            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(orders.getId());
            orderItem.setGoodsName(cart.getGoodName());
            orderItem.setPrice(cart.getPrice());
            orderItem.setImg(cart.getImg());
            orderItem.setNum(cart.getNum());
            orderItem.setGoodsId(cart.getGoodid());
            orderItemService.save(orderItem);

            // 下单成功后删除购物车商品
            shoppingcartService.removeById(cart.getId());
        }






    }

    @Override
    public void updateOrder(Orders orders) {
        if (Objects.equals(orders.getStatus(), OrderStatusEnum.CANCEL.getCode())) {
            // 取消订单，加库存
            Integer ordersId = orders.getId();
            List<OrderItem> orderItems = orderItemService.list(new QueryWrapper<OrderItem>().eq("order_id", ordersId));
            for (OrderItem orderItem : orderItems) {
                Mallgoods goods = mallgoodsService.getById(orderItem.getGoodsId());
                goods.setNums(goods.getNums() + orderItem.getNum());
                mallgoodsService.updateById(goods);
            }
        }
        updateById(orders);
    }


}




